//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SpreadBasketPayoff.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Payoff.h>
#include <gen/QL/Instruments/BasketPayoff.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CSpreadBasketPayoff::CSpreadBasketPayoff (Cephei::QL::IPayoff^ p) : CBasketPayoff(CSpreadBasketPayoff::typeid)
{
    CPayoff^ _Cp;
    try
    {
#ifdef HANDLE
        _phSpreadBasketPayoff = NULL;
#endif
        _Cp = safe_cast<CPayoff^> (p);
        _Cp->Lock();
        boost::shared_ptr<QuantLib::Payoff>& _p = static_cast<boost::shared_ptr<QuantLib::Payoff>&> (_Cp->GetShared ()); 
        _ppSpreadBasketPayoff = new boost::shared_ptr<QuantLib::SpreadBasketPayoff> (new QuantLib::SpreadBasketPayoff ( _p ));
        SetBasketPayoff (boost::dynamic_pointer_cast<QuantLib::BasketPayoff> (*_ppSpreadBasketPayoff));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cp != nullptr) _Cp->Unlock();
    }
}
Cephei::QL::Instruments::CSpreadBasketPayoff::CSpreadBasketPayoff (boost::shared_ptr<QuantLib::SpreadBasketPayoff>& childNative, Object^ owner) : CBasketPayoff(CSpreadBasketPayoff::typeid)
{
#ifdef HANDLE
	_phSpreadBasketPayoff = NULL;
#endif
	_ppSpreadBasketPayoff = &childNative;
    _ppBasketPayoff = new boost::shared_ptr<QuantLib::BasketPayoff> (boost::dynamic_pointer_cast<QuantLib::BasketPayoff> (*_ppSpreadBasketPayoff));
}
Cephei::QL::Instruments::CSpreadBasketPayoff::CSpreadBasketPayoff (QuantLib::SpreadBasketPayoff& childNative, Object^ owner) : CBasketPayoff(CSpreadBasketPayoff::typeid)
{
#ifdef HANDLE
	_phSpreadBasketPayoff = NULL;
#endif
	_ppSpreadBasketPayoff = new boost::shared_ptr<QuantLib::SpreadBasketPayoff> (&childNative);
    _ppBasketPayoff = new boost::shared_ptr<QuantLib::BasketPayoff> (boost::dynamic_pointer_cast<QuantLib::BasketPayoff> (*_ppSpreadBasketPayoff));
    _SpreadBasketPayoffOwner = owner;
    _BasketPayoffOwner = owner;
}

Cephei::QL::Instruments::CSpreadBasketPayoff::CSpreadBasketPayoff (CSpreadBasketPayoff^ copy) : CBasketPayoff(CSpreadBasketPayoff::typeid)
{
#ifdef HANDLE
	_phSpreadBasketPayoff = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSpreadBasketPayoff = new boost::shared_ptr<QuantLib::SpreadBasketPayoff> (copy->GetShared());
        _ppBasketPayoff = new boost::shared_ptr<QuantLib::BasketPayoff> (boost::dynamic_pointer_cast<QuantLib::BasketPayoff> (*_ppSpreadBasketPayoff));
    }
}
Cephei::QL::Instruments::CSpreadBasketPayoff::CSpreadBasketPayoff (PLATFORM::Type^ t) : CBasketPayoff(CSpreadBasketPayoff::typeid)
{
#ifdef HANDLE
	_phSpreadBasketPayoff = NULL;
#endif
	if (!t->IsSubclassOf(CSpreadBasketPayoff::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CSpreadBasketPayoff::CSpreadBasketPayoff (QuantLib::Handle<QuantLib::SpreadBasketPayoff>& childNative, Object^ owner)  : CBasketPayoff(CSpreadBasketPayoff::typeid)
{
	_phSpreadBasketPayoff = &childNative;
	_ppSpreadBasketPayoff = &static_cast<boost::shared_ptr<QuantLib::SpreadBasketPayoff>>(childNative.currentLink());
    _ppBasketPayoff = new boost::shared_ptr<QuantLib::BasketPayoff> (boost::dynamic_pointer_cast<QuantLib::BasketPayoff> (*_ppSpreadBasketPayoff));
    _SpreadBasketPayoffOwner = owner;
}
Cephei::QL::Instruments::CSpreadBasketPayoff::CSpreadBasketPayoff (QuantLib::Handle<QuantLib::SpreadBasketPayoff> childNative)  : CBasketPayoff(CSpreadBasketPayoff::typeid)
{
	_phSpreadBasketPayoff = &childNative;
	_ppSpreadBasketPayoff = &static_cast<boost::shared_ptr<QuantLib::SpreadBasketPayoff>>(childNative.currentLink());
    _ppBasketPayoff = new boost::shared_ptr<QuantLib::BasketPayoff> (boost::dynamic_pointer_cast<QuantLib::BasketPayoff> (*_ppSpreadBasketPayoff));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CSpreadBasketPayoff::CSpreadBasketPayoff (QuantLib::SpreadBasketPayoff childNative)  : CBasketPayoff(CSpreadBasketPayoff::typeid)
{
#ifdef HANDLE
	_phSpreadBasketPayoff = NULL;
#endif
	_ppSpreadBasketPayoff = new boost::shared_ptr<QuantLib::SpreadBasketPayoff> (new QuantLib::SpreadBasketPayoff (childNative));
    _ppBasketPayoff = new boost::shared_ptr<QuantLib::BasketPayoff> (boost::dynamic_pointer_cast<QuantLib::BasketPayoff> (*_ppSpreadBasketPayoff));
}
#endif

Cephei::QL::Instruments::CSpreadBasketPayoff::~CSpreadBasketPayoff ()
{
    if (_ppSpreadBasketPayoff != NULL)
    {
	    delete _ppSpreadBasketPayoff;
        _ppSpreadBasketPayoff = NULL;
    }
}
Cephei::QL::Instruments::CSpreadBasketPayoff::!CSpreadBasketPayoff ()
{
    if (_ppSpreadBasketPayoff != NULL)
    {
	    delete _ppSpreadBasketPayoff;
    }
}
QuantLib::SpreadBasketPayoff& Cephei::QL::Instruments::CSpreadBasketPayoff::GetReference ()
{
    if (_ppSpreadBasketPayoff == NULL) throw REFNEW NativeNullException ();
	return **_ppSpreadBasketPayoff;
}
boost::shared_ptr<QuantLib::SpreadBasketPayoff>& Cephei::QL::Instruments::CSpreadBasketPayoff::GetShared ()
{
    if (_ppSpreadBasketPayoff == NULL) throw REFNEW NativeNullException ();
	return *_ppSpreadBasketPayoff;
}
QuantLib::SpreadBasketPayoff* Cephei::QL::Instruments::CSpreadBasketPayoff::GetPointer ()
{
    if (_ppSpreadBasketPayoff == NULL) throw REFNEW NativeNullException ();
	return &**_ppSpreadBasketPayoff;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SpreadBasketPayoff>& Cephei::QL::Instruments::CSpreadBasketPayoff::GetHandle ()
{
	if (_phSpreadBasketPayoff == NULL)
	{
		_phSpreadBasketPayoff = new Handle<QuantLib::SpreadBasketPayoff> (*_ppSpreadBasketPayoff);
	}
	return *_phSpreadBasketPayoff;
}
#endif
bool Cephei::QL::Instruments::CSpreadBasketPayoff::HasNative () 
{
	return (_ppSpreadBasketPayoff != NULL);
}

Double Cephei::QL::Instruments::CSpreadBasketPayoff::Accumulate (Cephei::QL::Math::IArray^ a)
{
    CArray^ _Ca;
    try
    {
        _Ca = safe_cast<CArray^> (a);
        _Ca->Lock();
        QuantLib::Array& _a = static_cast<QuantLib::Array&> (_Ca->GetReference ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSpreadBasketPayoff)->accumulate ( _a );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ca != nullptr) _Ca->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::ISpreadBasketPayoff^ Cephei::QL::Instruments::CSpreadBasketPayoff_Factory::Create (Cephei::QL::IPayoff^ p)
{
    return REFNEW CSpreadBasketPayoff ( p);
}
